package hibernate.mappings;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

	private static String CONFIG_FILE_LOCATION = "hibernate.cfg.xml";

	private static final ThreadLocal threadLocal = new ThreadLocal();

	private static final Configuration config = new Configuration();

	private static SessionFactory sessionFactory;

	public Session currentSession() throws HibernateException {
		Session session = (Session) threadLocal.get();

		if (session == null) {
			if (sessionFactory == null) {
				try {
					config.configure(CONFIG_FILE_LOCATION);
					sessionFactory = config.buildSessionFactory();
				} catch (Exception e) {
					System.err
							.println("%%%%HibernateSessionFactory.java:   Error   Creating   SessionFactory   %%%% ");
					e.printStackTrace();
				}
			}
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}

		return session;
	}

	public static void closeSession() throws HibernateException {

		Session session = (Session) threadLocal.get();
		threadLocal.set(null);

		if (session != null) {
			session.close();
		}
	}

	public HibernateSessionFactory() {
	}

}